package com.itheima.controller;

import com.itheima.pojo.Admin;
import com.itheima.service.AdminService;
import com.itheima.service.impl.UserDiagramServiceImpl;
import com.itheima.vo.UserDistributionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("dashboard")
public class UserDistributionController {
    @Autowired
    private UserDiagramServiceImpl userDiagramServiceImpl;
    @Autowired
    private AdminService adminService;


    @GetMapping("distribution")
    public ResponseEntity<Object> distribution(@RequestHeader("Authorization") String token) {
        Admin admin = this.adminService.queryUserByToken(token);
        if (admin == null) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        try {
            UserDistributionVO distribution = userDiagramServiceImpl.distribution();
            return ResponseEntity.ok(distribution);
        } catch (Exception e) {
            log.error("数据获取错误", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }
}
